package das_java_forum_ide;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;

import java.awt.Color;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

public class newfile extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textField;
	private JTextField txtSrc;
	private JComboBox comboBox;
	private DefaultListModel Cmodel;
	private JList list;
	


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			newfile dialog = new newfile();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public newfile() {
		setTitle("Neue Datei Anlegen");
		setBounds(100, 100, 523, 357);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel, BorderLayout.CENTER);
			panel.setLayout(null);
			
			list = new JList();
			list.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "Daten Typ W\u00E4hlen", TitledBorder.CENTER, TitledBorder.TOP, null, null));
			list.setModel(new AbstractListModel() {
				String[] values = new String[] {"Java Klasse", "JFrame", "JDialog", "JApplet"};
				public int getSize() {
					return values.length;
				}
				public Object getElementAt(int index) {
					return values[index];
				}
			});
			list.setBounds(10, 111, 477, 154);
			panel.add(list);
			
			JLabel lblDateiName = new JLabel("Datei Name:");
			lblDateiName.setBounds(10, 11, 94, 14);
			panel.add(lblDateiName);
			
			textField = new JTextField();
			textField.setBounds(109, 8, 378, 20);
			panel.add(textField);
			textField.setColumns(10);
			
			comboBox = new JComboBox();
			comboBox.setBounds(109, 39, 378, 20);
			panel.add(comboBox);
			
			JLabel lblProjekt = new JLabel("Projekt:");
			lblProjekt.setBounds(10, 42, 46, 14);
			panel.add(lblProjekt);
			
			txtSrc = new JTextField();
			txtSrc.setText("\\src");
			txtSrc.setColumns(10);
			txtSrc.setBounds(109, 68, 378, 20);
			panel.add(txtSrc);
			
			JLabel lblOrdner = new JLabel("Ordner:");
			lblOrdner.setBounds(10, 71, 94, 14);
			panel.add(lblOrdner);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						
						String path = comboBox.getSelectedItem() + txtSrc.getText() + "\\" +  textField.getText(); 
						String packet = new File(comboBox.getSelectedItem().toString()).getName();
						String value = "package " + packet + "\n \n \n calss " + textField.getText() + "{\n \n \n \n public void main(){ \n // hier ihr code \n \n \n \n \n} \n \n \n }"; 
						
						// inhalt erstellen
						
						if(list.getSelectedValue()=="Java Klasse"){
							path = path + ".class";
						}
						
						
						File f = new File(path);
						
						
						try {
							
							
							FileWriter fw = new FileWriter(f);
							BufferedWriter bw = new BufferedWriter(fw);
							bw.write(value);
							bw.close();
							
							
							
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
						Main.loadProjekts();
						
						
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		importsprojekt();
	}
	
	public void importsprojekt(){
		
		Projektverwaltung.Main pv = new Projektverwaltung.Main();
		ArrayList projects = pv.getProjekts(Main.WorkingDir);
		for(int i = 0 ; i < projects.size() ; i++ ){
			
			comboBox.addItem(projects.get(i));
			
		}
	}
	
}
